using System.Text;

namespace Test.ConsoleProgram.Algorithm.Solution
{
    [TestDescription("算法: 0012. 整数转罗马数字")]
    public class No0012_IntToRoman : AbsBaseTestItem
    {
        public override void OnTest()
        {
            Assert.TestExe(IntToRoman, 3, "III");
            Assert.TestExe(IntToRoman, 4, "IV");
            Assert.TestExe(IntToRoman, 9, "IX");
            Assert.TestExe(IntToRoman, 58, "LVIII");
            Assert.TestExe(IntToRoman, 1994, "MCMXCIV");
        }

        public string IntToRoman(int num)
        {
            int[] values = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };
            string[] symbols = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
            StringBuilder sb = new StringBuilder();
            // Loop through each symbol, stopping if num becomes 0.
            for (int i = 0; i < values.Length && num >= 0; i++)
            {
                // Repeat while the current symbol still fits into num.
                while (values[i] <= num)
                {
                    num -= values[i];
                    sb.Append(symbols[i]);
                }
            }
            return sb.ToString();
        }
    }
}
